﻿namespace Magic.System;

[Injection]
public class CacheService : ICacheService
{
    private readonly IMemberCacheService _cache;

    public CacheService(IMemberCacheService cache)
    {
        this._cache = cache;
    }


    /// <summary>
    /// 获取所有权限
    /// </summary>
    /// <returns></returns>
    public async Task<List<string>> GetAllPermission()
    {
        var cacheKey = CacheConst.KeyPermission;
        return await _cache.Get<List<string>>(cacheKey);
    }
    /// <summary>
    /// 获取权限缓存（按钮）
    /// </summary>
    /// <param name="userid"></param>
    /// <returns></returns>
    public async Task<List<string>> GetPermission(long userid)
    {
        var userId = UserManager.UserId;
        var cacheKey = CacheConst.KeyPermission + $"{userId}";
        return await _cache.Get<List<string>>(cacheKey);
    }
    /// <summary>
    /// 设置所有按钮权限
    /// </summary>
    /// <param name="permissions"></param>
    /// <returns></returns>
    public async Task SetAllPermission(List<string> permissions)
    {
        var cacheKey = CacheConst.KeyPermission;
        await _cache.Set(cacheKey, permissions);
    }
    /// <summary>
    /// 缓存权限
    /// </summary>
    /// <param name="userId"></param>
    /// <param name="permissions"></param>
    /// <returns></returns>
    public async Task SetPermission(long userId, List<string> permissions)
    {
        var cacheKey = CacheConst.KeyPermission + $"{userId}";
        await _cache.Set(cacheKey, permissions);
    }
}
